package com.srcbox.file.ui

import android.content.DialogInterface
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.srcbox.file.R
import com.srcbox.file.data.AppSetting
import com.srcbox.file.util.EggUtil
import kotlinx.android.synthetic.main.error_activity.*
import java.io.File

class ErrorCrash : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.error_activity)
        EggUtil.alterStateBarColor(this, "#333333")
        val gradientDrawable = GradientDrawable()
        gradientDrawable.cornerRadius = 25f
        gradientDrawable.setStroke(1, Color.parseColor("#333333"))
        gradientDrawable.setColor(Color.parseColor("#333333"))
        error_copy_btn.background = gradientDrawable
        error_copy_btn.setOnClickListener {
            val log = CustomActivityOnCrash.getAllErrorDetailsFromIntent(this, intent)
            val alertDialog = AlertDialog.Builder(this).setTitle("Error Log").setMessage(log)
                .setNegativeButton("复制") { _: DialogInterface, _: Int ->
                    EggUtil.copyText(this, "Android Sdk ${Build.VERSION.SDK_INT}\n$log")
                    EggUtil.toast(this, "已复制")
                }
            alertDialog.setNeutralButton("联系开发者") { _: DialogInterface, _: Int ->
                EggUtil.copyText(this, log)
                EggUtil.toast(this, "已复制LOG")
                EggUtil.goQQ(this, "1970284668")
            }
            alertDialog.show()
        }

        errorJiaQun.setOnClickListener {
            EggUtil.joinQQGroup(this,AppSetting.QUN_KEY)
        }

        error_repair_btn.setOnClickListener {
            EggUtil.deletes(File("$filesDir"))
            startActivity(Intent(this, Welcome::class.java))
            finish()
        }
    }
}